package com.example.hospital.patient.wx.api.Service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.hospital.patient.wx.api.Service.UserService;
import com.example.hospital.patient.wx.api.db.dao.UserDao;
import com.example.hospital.patient.wx.api.db.dao.UserInfoCardDao;
import com.example.hospital.patient.wx.api.db.pojo.UserEntity;
import com.example.hospital.patient.wx.api.db.pojo.UserInfoCardEntity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @author Baiken
 * @version 1.0
 * @description: 用户业务实现层
 * @date 2024/9/6 下午3:08
 */
@Service
public class UserServiceImpl implements UserService {

    @Value("${wechat.app-id}")
    private String appId;

    @Value("${wechat.app-secret}")
    private String appSecret;

    @Resource
    private UserDao userDao;

    @Resource
    private UserInfoCardDao userInfoCardDao;

    @Override
    public HashMap loginOrRegister(String code, String nickname, String photo, String sex) {

        String openId = this.getOpenId(code);
        HashMap map = new HashMap<>();
        Integer id = userDao.searchAlreadyRegistered(openId);
        if(id == null){
            UserEntity entity = new UserEntity();
            entity.setOpenId(openId);
            entity.setNickname(nickname);
            entity.setPhoto(photo);
            entity.setSex(sex);
            entity.setStatus((byte)1);
            userDao.insert(entity);
            id = userDao.searchAlreadyRegistered(entity.getOpenId());
            map.put("msg","注册成功");
        }else {
            map.put("msg","登录成功");
        }
        String tel = userInfoCardDao.searchUserTel(id);
        map.put("id",id);
        map.put("tel",tel);
        return map;
    }

    @Override
    public HashMap searchUserInfo(int userId) {
        HashMap map = userDao.searchUserInfo(userId);
        String tel = userInfoCardDao.searchUserTel(userId);
        map.put("tel",tel);
        return map;
    }

    private String getOpenId(String code){
        String url = "https://api.weixin.qq.com/sns/jscode2session";
        System.out.println("---------------");
        System.out.println("code = " + code);
        System.out.println("----------------");
        HashMap map = new HashMap();
        map.put("appid",appId);
        map.put("secret",appSecret);
        map.put("js_code",code);
        map.put("grant_type","authorization_code");
        String response = HttpUtil.post(url,map);
        System.out.println("---------------");
        System.out.println("response = " + response);
        System.out.println("----------------");
        JSONObject json = JSONUtil.parseObj(response);
        String openId = json.getStr("openid");
        if(openId == null || openId.length() == 0)
            throw new RuntimeException("临时登录凭证错误");
        return openId;
    }
}
